package cz.fit.vutbr.pis2012.augeaswh.control.ekonom.stock_types;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import cz.fit.vutbr.pis2012.augeaswh.managers.StockTypesManager;
import cz.fit.vutbr.pis2012.augeaswh.persistence.StockType;

@ManagedBean
@SessionScoped
public class StockTypesEditorBean implements Serializable {
	@EJB
	private StockTypesManager sm;
	
	private static final long serialVersionUID = -6000365533104340973L;
	private StockType stockType;
	private String name;
	private String productClass;
	private BigDecimal price;

	public StockTypesEditorBean() {
		setStockType(null);
	}
	
	public StockType getStockType() {
		return stockType;
	}

	public void setStockType(StockType stockType) {
		this.stockType = stockType;
		name=productClass="";
		price=new BigDecimal(0);
		if(this.stockType!=null) {
			this.name=stockType.getName();
			this.productClass=stockType.getProductClass();
			this.price=stockType.getPrice();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProductClass() {
		return productClass;
	}

	public void setProductClass(String productClass) {
		this.productClass = productClass;
	}
	
	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public String submit() {
		
		if(stockType==null) {
			this.stockType=new StockType();
		}
		this.stockType.setName(name);
		this.stockType.setProductClass(productClass);
		this.stockType.setPrice(price);
		sm.save(stockType);
		setStockType(null);
		return "back";
	}
	
	public String getTitle() {
		if(this.stockType==null) {
			return "New stock type";
		}
		else {
			return "Edited stock type \""+this.stockType.getName()+"\"";
		}			
	}
}
